import { getToken, clearToken } from '../util/token.js';
import config from '../config/index';

const baseUrl = 'http://127.0.0.1:80/api'
const token_type = config.COOKIE_TYPE;

var instance = (config_) => {
	return uni.request({
		url: baseUrl + config_.url,
		method: config_.method,
		data: config_.data,
		header: {
		        'qa-token': getToken(),//自定义请求头信息
				'Content-Type': typeof(config_['Content-Type']) !== 'undefined' ? config_['Content-Type']: 'application/json',
		}
	}).then(res => {
		// console.log(res[1])
		// console.log(res[1].data.code === 200)
			// console.log(res[1].data.body)
		return new Promise((resolve, reject) => {
			if (res[1].data.code === 200) {
				return resolve(res[1].data.body)
			}
			uni.showToast({
				title: res[1].data.message,
			})
			if (res[1].data.code === 401) {
				uni.hideTabBar({
				})
				clearToken();
				uni.reLaunch({
					url: 'pages/login/index',
				})
			}
			return reject([1].data);
		})
	})
}

export default instance;